<template>
  <div class="header">
    <div>
      <h3 class="title">叩丁狼电商运营平台</h3>
      <el-icon color="white" :size="20" @click="isCollapse = !isCollapse"
        ><Menu
      /></el-icon>
    </div>
    <el-dropdown @command="handle">
      <span class="el-dropdown-link">
        {{ userStore.username }}
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="index">首页</el-dropdown-item>
          <el-dropdown-item command="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup lang="ts">
import { Menu, ArrowDown } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { inject } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/user";
import { logout } from "@/common";
import { logoutApi } from "../api";
//获取依赖注入的数据
const isCollapse = inject("isCollapse");
//仓库数据
const userStore = useUserStore();
//获取路由
const router = useRouter();
const handle = (command: string) => {
  if (command === "index") {
    //跳转到首页
    router.push("/index");
  } else if (command === "logout") {
    //退出登录
    logout(
      () => {
        //退出请求
        logoutApi();
      },
      () => {
        //5.提示
        ElMessage.success("退出成功");
      }
    );
  }
};
</script>

<style scoped lang="less">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  //标题
  .title {
    margin-right: 20px;
    display: inline-block;
    line-height: 70px;
    font-weight: 400;
    font-size: 24px;
    color: #fff;
  }
  //下拉框样式
  :deep(.el-dropdown) {
    color: white;
  }
  .example-showcase .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
  }
}
</style>
